package com.example.smartcity1.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.smartcity1.R;
import com.example.smartcity1.bean.NewsBean;

import java.util.List;

import static com.example.smartcity1.network.RetrofitClient.IP;

public class NewsHomeAdapter extends ArrayAdapter<NewsBean.RowsDTO> {
    public NewsHomeAdapter(@NonNull Context context, int resource, @NonNull List<NewsBean.RowsDTO> objects) {
        super(context, resource, objects);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_news_home,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.img = convertView.findViewById(R.id.item_home_news_img);
            viewHolder.title = convertView.findViewById(R.id.item_home_news_title);
            viewHolder.text = convertView.findViewById(R.id.item_home_news_text);
            viewHolder.time = convertView.findViewById(R.id.item_home_news_time);
            viewHolder.comment = convertView.findViewById(R.id.item_home_news_comment);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        NewsBean.RowsDTO rowsDTO = getItem(position);
        Glide.with(getContext()).load(IP + rowsDTO.getImgUrl())
                .placeholder(R.drawable.ic_baseline_cloud_download_24)
                .centerCrop()
                .into(viewHolder.img);
        viewHolder.title.setText(rowsDTO.getTitle());
        viewHolder.text.setText(rowsDTO.getContent());
        viewHolder.time.setText(rowsDTO.getCreateTime().toString());
        viewHolder.comment.setText(rowsDTO.getLikeNumber().toString());
        return convertView;
    }
    private class ViewHolder {
        ImageView img;
        TextView title;
        TextView text;
        TextView time;
        TextView comment;
    }
}
